<?php
class PluginMgr extends Action {
	/*
	 * (non-PHPdoc) @see Action::execute()
	 */
	public function execute($params) {
		$me = check_login ( ADMIN_LOGIN_PAGE );
		$data = array ();
		$group = rqst ( 'group', 'uninstall' );
		$plgMgr = PluginManager::getInstance ();
		$plugins1 = $plgMgr->getPlugins ( false );
		$plugins2 = $plgMgr->getPlugins ();
		$etotal = $dtotal = 0;
		foreach ( $plugins2 as $p ) {
			if ($p ['disabled']) {
				$dtotal ++;
			} else {
				$etotal ++;
			}
		}
		$data ['installedTotal'] = count ( $plugins2 );
		$data ['uninstalledTotal'] = count ( $plugins1 );
		$data ['dtotal'] = $dtotal;
		$data ['etotal'] = $etotal;
		$data ['items'] = $group == 'uninstall' ? $plugins1 : $plugins2;
		$data ['group'] = $group;
		
		$data ['uploadable'] = extension_loaded ( 'zip' ) && $plgMgr->enabled ( 'cn.usephp.filemgr',false );
		bind ( 'get_plugin_operation', array ($this, 'get_plugin_operation' ), 10, 2 );
		return new SmartyView ( $data, 'dashboard/PluginMgr' );
	}
	
	public function get_plugin_operation($ops, $item) {
		if ($item ['Installed']) { // 已经安装
			if ($item ['disabled']) {
				$ops .= '<a href="#' . $item ['Plugin_ID'] . '" class="btn-disabled" data-content="0">启用</a>';
			} else {
				$ops .= '<a href="#' . $item ['Plugin_ID'] . '" class="btn-disabled" data-content="1">禁用</a>';
			}
			if ($item ['Option']) {
				$ops .= '<a href="#' . $item ['Option'] . '" class="setting" data-content="' . $item ['Plugin_ID'] . '">设置</a>';
			}
			$ops .= '<a href="#' . $item ['Plugin_ID'] . '" class="uninstall">卸载</a>';
		} else {
			$ops .= '<a href="#' . $item ['Plugin_ID'] . '" class="install">安装</a>';
		}
		return $ops;
	}
}